package br.com.playonline.rest;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import br.com.playonline.bussines.EnderecoBU;
import br.com.playonline.domain.Endereco;
import br.com.playonline.domain.Usuario;
import br.com.playonline.dto.UsuarioDTO;

@RestController
public class RestSpring {

	@Autowired
	SessionFactory factory;

	@Autowired
	EnderecoBU enderecoBU;
	
	@RequestMapping(value = "/rest/usuario", method = RequestMethod.GET)
	public @ResponseBody
	Usuario getDummyEmployee() {
		// logger.info("Start getDummyEmployee");
		// Employee emp = new Employee();
		// emp.setId(9999);
		// emp.setName("Dummy");
		// emp.setCreatedDate(new Date());
		// empData.put(9999, emp);
		List<Endereco> enderecos = enderecoBU.findAll();
//		this.enderecoBU.save(new Endereco());
		Usuario usuario = new Usuario();
		usuario.setId(new Long(1));
		usuario.setEmail("soudaniel@gmail.com");
		return usuario;
	}
	
	@RequestMapping(value = "/rest/incluir", method = RequestMethod.POST)
    public @ResponseBody
    void inclui(@RequestBody UsuarioDTO usuario) {
		System.out.println(usuario.getNome());
	}
	
	@RequestMapping(value = "/rest/inserir",method = RequestMethod.POST)
	public @ResponseBody void inserirUsuario(@RequestBody UsuarioDTO usuarioDTO){
		System.out.println("ID:"+usuarioDTO.getId());
		System.out.println("Nome: "+usuarioDTO.getNome());
		System.out.println("CPF:"+usuarioDTO.getCpf());
	}
	
	
	@RequestMapping(value = "/rest/teste/{id}",method = RequestMethod.GET)
	public String getTeste(@PathVariable String id){
		System.out.println(id);
		return "ola";
	}
	
	@RequestMapping(value = "/rest/usuarios", method = RequestMethod.GET)
	public @ResponseBody List<Usuario> findAll(){
		
		List<Usuario> usuarios = this.factory.openSession()
				.createCriteria(Usuario.class).list();
		return usuarios;
	}
	

}
